#include "userMain.h"
#include "userIRsend.h"
/****
uint8_t userIR_Code[200]={0x1C,0x26,0x15,0x34,0x15,0x7E,\
    0x04,0xE0,0x00,0x01,0x17,\
    0x0A,0xFF,0x00,0xFB,0x04,0xEB,0x14,0xFF,0x00,0xB4,0x4B,\
    0x05,0x15,0x00,0x01,0x26,0x15,};*/

//Command on
      /***
uint8_t userIR_Code[500]={0x1A,0x26,0x11,0x2E,0x11,0x10,\
    0x02,0x83,0x40,\
    0x0E,0x3B,0x2C,0x9B,0x7F,0xFF,0xDB,0x3F,0x5F,0xE3,0xFF,0xFF,0xFF,0xFF,0x61,\
    0x01,0x11,};
uint8_t userIR_DecodeB[500] = {131,64,\
    17,46,17,46,17,16,17,16,17,16,17,46,17,16,17,16,17,46,17,46,17,16,17,46,17,16,17,16,17,46,17,46,\
    17,16,17,46,17,46,17,16,17,16,17,46,17,16,17,16,17,46,17,16,17,16,17,16,17,16,17,16,17,16,17,16,\
    17,16,17,16,17,16,17,16,17,16,17,16,17,16,17,16,17,16,17,16,17,46,17,16,17,16,17,46,17,16,17,16,\
    17,46,17,46,17,16,17,16,17,16,17,16,17,16,17,16,17,46,17,16,17,46,17,16,17,16,17,16,17,16,17,16,\
    17,16,17,16,17,16,17,46,17,46,17,46,17,16,17,16,17,16,17,16,17,16,17,16,17,16,17,16,17,16,17,16,\
    17,16,17,16,17,16,17,16,17,16,17,16,17,16,17,16,17,16,17,16,17,16,17,16,17,16,17,16,17,16,17,16,\
    17,16,17,16,17,16,17,16,17,16,17,16,17,16,17,16,17,46,17,16,17,16,17,46,17,46,17,46,17,46,17,16,\
    17,};***/
/***
uint8_t userIR_Code[500]={0x40,0x26,0x17,0x16,0x17,0x40,\
0x04,0x00,0x01,0x54,0xAB,\
0x04,0x9E,0x50,0x04,0x0A,\
0x0A,0x17,0x16,0x17,0x40,0x17,0x16,0x17,0x00,0x02,0xF6,\
0x04,0x4B,0x04,0x00,0x03,\
0x08,0x17,0x00,0x05,0xE2,0x00,0x01,0x53,0xAB,\
0x04,0x9E,0x50,0x04,0x0E,\
0x0A,0x17,0x16,0x17,0x40,0x17,0x16,0x17,0x00,0x02,0xF7,\
0x04,0x00,0x00,0x00,0x0B,\
0x01,0x17,};***/

uint8_t userIR_Code[100]={0x40,0x26,0x17,0x16,0x17,0x40,\
0x04,0x00,0x01,0x54,0xAB,\
0x04,0x9E,0x10,0x04,0x0A,\
0x0A,0x17,0x16,0x17,0x40,0x17,0x16,0x17,0x00,0x02,0xF6,\
0x04,0x8B,0x04,0x00,0x05,\
0x08,0x17,0x00,0x05,0xE2,0x00,0x01,0x53,0xAB,\
0x04,0x9E,0x10,0x04,0x0E,\
0x0A,0x17,0x16,0x17,0x40,0x17,0x16,0x17,0x00,0x02,0xF7,\
0x04,0x00,0x00,0x00,0x0D,\
0x01,0x17,};

//uint8_t userIR_DecodeB[300];
uint8_t userIR_Decode[400];

/***
uint8_t userIR_DecodeB[500];
uint8_t userIR_Decode[500] = {0,1,93,169,27,61,27,18,27,18,\
 27,60,27,19,26,60,27,19,26,19,\
 26,61,26,60,27,60,26,19,27,19,\
 26,19,26,19,26,19,26,19,26,19,\
 26,19,26,19,26,19,26,62,26,19,\
 26,19,26,19,26,19,26,19,26,19,\
 26,62,25,20,25,63,26,19,26,19,\
 25,63,26,19,25, 0, 3, 0,26,19,\
 26,19,26,19,26,19,25,20,26,19,\
 25,20,25,20,25,20,25,20,25,20,\
 25,20,25,20,25,63,25,20,25,20,\
 25,20,25,20,25,20,25,20,25,20,\
 25,20,25,20,25,20,25,20,25,20,\
 25,20,25,20,25,20,25,20,25,63,\
 25,63,25, 0, 6, 1, 0, 1,92,171,\
 26,62,26,19,26,19,26,62,26,19,\
 26,62,25,20,25,20,25,63,25,63,\
 25,63,25,20,25,20,25,20,25,20,\
 25,20,25,20,25,20,25,20,25,20,\
 25,20,25,63,25,20,25,20,25,20,\
 25,20,25,20,25,20,25,63,25,63,\
 25,63,25,20,25,20,25,63,25,20,\
 25, 0, 3, 0,25,20,25,20,25,20,\
 25,20,25,20,25,20,25,20,25,20,\
 25,20,25,20,25,20,25,20,25,20,\
 25,20,25,20,25,20,25,20,25,20,\
 25,20,25,20,25,20,25,63,25,20,\
 25,20,25,20,25,20,25,20,25,20,\
 25,20,25,20,25,63,25,63,25,};   ***/

//"command off"
/*
uint8_t userIR_Code[500]={0x1A,0x26,0x11,0x2E,0x11,0x10,\
    0x02,0x83,0x40,\
    0x0E,0x3B,0x2C,0x9B,0x7F,0xFF,0xFB,0x3F,0x5F,0xE3,0xFF,0xFF,0xFF,0xFF,0x51,\
    0x01,0x11,};

uint8_t userIR_DecodeB[500] =   {131,64,\
    17,46,17,46,17,16,17,16,17,16,17,46,17,16,17,16,17,46,17,46,17,16,17,46,17,16,17,16,17,46,17,46,\
    17,16,17,46,17,46,17,16,17,16,17,46,17,16,17,16,17,46,17,16,17,16,17,16,17,16,17,16,17,16,17,16,\
    17,16,17,16,17,16,17,16,17,16,17,16,17,16,17,16,17,16,17,16,17,16,17,16,17,16,17,46,17,16,17,16,\
    17,46,17,46,17,16,17,16,17,16,17,16,17,16,17,16,17,46,17,16,17,46,17,16,17,16,17,16,17,16,17,16,\
    17,16,17,16,17,16,17,46,17,46,17,46,17,16,17,16,17,16,17,16,17,16,17,16,17,16,17,16,17,16,17,16,\
    17,16,17,16,17,16,17,16,17,16,17,16,17,16,17,16,17,16,17,16,17,16,17,16,17,16,17,16,17,16,17,16,\
    17,16,17,16,17,16,17,16,17,16,17,16,17,16,17,16,17,46,17,16,17,46,17,16,17,46,17,46,17,46,17,16,\
    17,};*/
void userIrInitFunc(void)
{
    GPIO_InitTypeDef        GPIO_InitStructure;
    /* GPIOA Periph clock enable */
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
	
    /* Configure PA12 in output pushpull mode */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    GPIO_ResetBits(GPIOA,GPIO_Pin_12);
}

#define     IR_LEVEL_H      GPIO_SetBits(GPIOA,GPIO_Pin_12)
#define     IR_LEVEL_L      GPIO_ResetBits(GPIOA,GPIO_Pin_12)

static uint8_t userCarrierFlag = 0;
uint8_t userIrSendFlag=1;
uint8_t userIrSendEnable = 0;
extern uint8_t userIRprotect;
void userIrSendStart(void)
{
    GPIO_SetBits(GPIOA,GPIO_Pin_12);
    userIrSendEnable = 1;
    userIRprotect=1;
    uint16_t userIrFrequency = 0;
    //TIM_Cmd(TIM3, DISABLE);
    userIrFrequency = ((uint16_t)userIR_Code[1]) * 1000;
    if (SysTick_Config((SystemCoreClock / 2) /userIrFrequency))
    { 
      /* Capture error */ 
      while (1);
    }
}

void userIrSendEnd(void)
{
    GPIO_ResetBits(GPIOA,GPIO_Pin_12);
    userIrSendEnable = 0;
    //TIM_Cmd(TIM3, ENABLE);
    if (SysTick_Config(SystemCoreClock / 1000))
    { 
      /* Capture error */ 
      while (1);
    }
}

uint16_t userIrSendCnt = 0;
void userIrSendCarrier(void)
{
    userIrSendCnt--;
    if(userIrSendEnable==0)
        return;
    if(userIrSendFlag)
    {
        if(userCarrierFlag)
        {
            userCarrierFlag = 0;
            IR_LEVEL_L;
        }
        else
        {
            userCarrierFlag = 1;
            IR_LEVEL_H;
        }
    }
    else
    {
        IR_LEVEL_L;
    }
}

uint16_t userIrDecodeLength = 289;
uint8_t userIrDecodeErrorFlag=0;
void userIrDecodeFunc(void)
{
    uint16_t user_m,user_n;
    uint16_t userCodeLength;
    uint8_t userIrDecodeFlag=1;
    uint16_t userDataLen,user_i,user_j;
    uint8_t userData;

    userCodeLength = userIR_Code[0];
    user_m = 6;
    user_n = 0;
    userIrDecodeErrorFlag=0;
    while(userCodeLength > user_m)
    {
        if(userIrDecodeFlag)
        {
            userIrDecodeFlag = 0;
            userDataLen = userIR_Code[user_m++];
            if((user_m + userDataLen) > userCodeLength)
            {
                userIrDecodeErrorFlag=1;
                return ;
            }
            for(user_i=0;user_i<userDataLen;user_i++)
            {
                userIR_Decode[user_n+user_i] = userIR_Code[user_m+user_i];
            }
            user_n += user_i;
            user_m += user_i;
        }
        else
        {
            userIrDecodeFlag = 1;
            userDataLen = userIR_Code[user_m++];
            if((user_m + userDataLen) > userCodeLength)
            {
                userIrDecodeErrorFlag=1;
                return ;
            }
            for(user_i=0;user_i<userDataLen;user_i++)
            {
                userData = userIR_Code[user_m+user_i];
                for(user_j=0;user_j<8;user_j++)
                {
                     if(userData & 0x80)
                     {
                        userIR_Decode[user_n+user_j*2]= userIR_Code[4];
                        userIR_Decode[user_n+user_j*2+1]= userIR_Code[5];
                     }
                     else
                     {
                        userIR_Decode[user_n+user_j*2]= userIR_Code[2];
                        userIR_Decode[user_n+user_j*2+1]=userIR_Code[3];
                     }
                     userData <<= 1;
                }
                user_n += 16;
            }
            user_m += user_i;
        }
        _NOP;
    }
    userIrDecodeLength = user_n;
    _NOP;
    /**for(user_i =0;user_i<userIrDecodeLength;user_i++)
    {
        if(userIR_DecodeB[user_i]!= userIR_Decode[user_i])
          _NOP;
    }**/
    _NOP;
}

void userIrSendDataCodeFunc(void)
{
    uint16_t userLevelTime;
    uint16_t user_m=0;
    uint8_t userIrDataFlag=1;
    while(user_m < userIrDecodeLength)
    {
        if(userIR_Decode[user_m]==0)
        {
            userLevelTime = userIR_Decode[user_m+1];
            userLevelTime = userLevelTime * 256 + userIR_Decode[user_m+2];
            user_m += 3;
        }
        else
        {
            userLevelTime = userIR_Decode[user_m++];
        }
        if(userIrDataFlag)
        {
            userIrDataFlag = 0;
            userIrSendCnt = userLevelTime * 2;
            userIrSendFlag = 1;
            
        }
        else
        {
            userIrDataFlag = 1;
            userIrSendCnt = userLevelTime * 2;
            userIrSendFlag = 0;
        }
        _NOP;
        while(userIrSendCnt);
        _NOP;
    }
    _NOP;
}

//void userIrSendFunc(uint8_t userIrZipFlag)
void userIrSendFunc(void)
{
    /****if(userIrZipFlag)
    {
        userIrDecodeFunc();  
        userIrZipFlag = 0;
    }*******************/
    if(userIrDecodeErrorFlag)
    {
        return ;
    }
    userLedCtrlFunc(0x0d);
    userIrSendStart();
    userIrSendDataCodeFunc();
    userIrSendEnd();
}
